<?php


namespace app\home\model\page\circle;

use app\api\model\dao\Circle;
use app\library\Error;
use app\library\PageBase;
use think\Exception;

class AddLogo extends PageBase
{
    private $objCircleModel;

    public function __construct()
    {
        parent::__construct();
        $this->objCircleModel = new Circle();
    }

    public function excute($param)
    {
        $ret = $this->checkToken($param);
        if (false === $ret) {
            //return $this->getRes(Error::ERR_NO_AUTH);
        }

        $ret = $this->checkParam($param);
        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }

        $data = [
            'id' => $param['cir_id'],
            'icon' => $param['logo'],
        ];
        try {
            $this->objCircleModel->updateCircle($data);
            return $this->getRes();
        } catch (Exception $e) {
            return $this->getRes(Error::ERR_SYS, $e->getMessage());
        }
    }

    public function checkParam($param)
    {
        $rule = [
            'cir_id' => 'require',
            'logo' => 'require',
        ];
        $msg = [
            'cir_id.require' => '圈子ID必须',
            'logo.require' => '圈子logo必须',
        ];

        $validate = new \think\Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }
}